import { createApp } from "vue";
import App from "./App.vue";
import store from "./store";
import router from "./router";
import ElementPlus from "element-plus";
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
import "element-plus/theme-chalk/index.css";
import "element-plus/theme-chalk/dark/css-vars.css";
import "element-plus/dist/index.css";
import mitt from "mitt";
import "./assets/main.css";
const app = createApp(App);
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
app.directive("click-outside", {
  mounted(el, binding) {
    function eventHandler(e) {
      if (el.contains(e.target)) {
        return false;
      }
      // 如果绑定的参数是函数，正常情况也应该是函数，执行
      if (binding.value && typeof binding.value === "function") {
        binding.value(e);
      }
    }
    // 用于销毁前注销事件监听
    el.__click_outside__ = eventHandler;
    // 添加事件监听
    document.addEventListener("click", eventHandler);
  },
  beforeUnmount(el) {
    // 移除事件监听
    document.removeEventListener("click", el.__click_outside__);
    // 删除无用属性
    delete el.__click_outside__;
  },
});
app.use(store);
app.use(router);
app.use(ElementPlus);

const bus = mitt();
app.config.globalProperties.$bus = bus;
app.mount("#app");
